package com.itheima.user.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.entity.dto.LikeDto;
import com.itheima.entity.pojo.Like;
import com.itheima.entity.pojo.User;
import com.itheima.entity.vo.LikeVo;
import com.itheima.entity.vo.PagingQueryVo;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.user.mapper.LikeMapper;
import com.itheima.user.service.LikeService;
import com.itheima.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2022-12-15 20:33
 * description :
 */
@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private UserService userService;

    @Override
    public Boolean insertLike(LikeDto likeDto) {
        if (StringUtils.isEmpty(likeDto.getLikeId())){
            throw new RuntimeException("喜欢对象的ID不能为空");
        }
        if (likeDto.getLikeId().equals(ThreadLocalUtil.getId())){
            throw new RuntimeException("喜欢对象不能是自己");
        }
        User user=userService.selectById(likeDto.getLikeId());
        if (ObjectUtil.isEmpty(user)){
            throw new RuntimeException("喜欢的对象不存在");
        }
        Like likeInDb=likeMapper.selectByUserIdAndLikeId(ThreadLocalUtil.getId(),likeDto.getLikeId());
        if (!ObjectUtil.isEmpty(likeInDb)){
            throw new RuntimeException("喜欢的关系已存在");
        }
        Like like = new Like();
        BeanUtils.copyProperties(likeDto,like);
        like.setId(IdUtil.getSnowflakeNextIdStr());
        like.setUserId(ThreadLocalUtil.getId());
        like.setCreateTime(LocalDateTime.now());
        return  likeMapper.insertLike(like);
    }

    @Override
    public PagingQueryVo<LikeVo> selectPage(String name, Integer type, Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        List<LikeVo> like;
        Integer total;
        if (type==1){
             like=likeMapper.selectPage1(name,start,pageSize,ThreadLocalUtil.getId());
             total=likeMapper.countPage1(name,ThreadLocalUtil.getId());
        }else if (type==2){
             like=likeMapper.selectPage2(name,start,pageSize,ThreadLocalUtil.getId());
            total=likeMapper.countPage2(name,ThreadLocalUtil.getId());
        }else {
             like=likeMapper.selectPage3(name,start,pageSize,ThreadLocalUtil.getId());
            total=likeMapper.countPage3(name,ThreadLocalUtil.getId());
        }
        PagingQueryVo<LikeVo> likeVoPagingQueryVo = new PagingQueryVo<>();
        likeVoPagingQueryVo.setData(like);
        likeVoPagingQueryVo.setPage(page);
        likeVoPagingQueryVo.setPageSize(pageSize);
        likeVoPagingQueryVo.setTotal(total);
        likeVoPagingQueryVo.setPages(total/pageSize+1);
        return likeVoPagingQueryVo;
    }

    @Override
    public Boolean deleteLike(String likeId) {
        Like like = likeMapper.selectByUserIdAndLikeId(ThreadLocalUtil.getId(), likeId);
        if (ObjectUtil.isEmpty(like)){
            throw new RuntimeException("喜欢列表不存在该喜欢对象");
        }
        return likeMapper.deleteLike(ThreadLocalUtil.getId(),likeId);
    }
}
